''' <summary>
''' This is the control that is responsible for rendering a specific tab.
''' </summary>
Friend Class TabPageControl
    Inherits TabBaseControl

#Region "Initialization"
    Private WithEvents Page As TabPage
    Public Sub New(ByVal page As TabPage)
        Me.Page = page

        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        
        Me.InitializeClose()
    End Sub
#End Region

#Region "Close / Selection Logic"
    Public Event OnClose(ByVal page As TabPage)

    Private WithEvents CloseLink As Label

    Private Sub InitializeClose()
        Me.CloseLink = New Label()
        Me.CloseLink.AutoSize = False
        Me.CloseLink.Dock = DockStyle.Right
        Me.CloseLink.Font = New Font(New FontFamily("Microsoft Sans Serif"), 8.25, FontStyle.Bold, GraphicsUnit.Point)
        Me.CloseLink.Visible = False
        Me.CloseLink.Width = 16
        Me.CloseLink.BackColor = Color.Transparent
        Me.CloseLink.Text = "X"
        Me.CloseLink.TextAlign = ContentAlignment.MiddleCenter
        Me.Controls.Add(Me.CloseLink)
    End Sub

    Public Sub Close()
        RaiseEvent OnClose(Me.Page)
    End Sub

    Private Sub CloseLink_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CloseLink.Click
        Close()
    End Sub

    Private Sub CloseLink_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles CloseLink.MouseEnter
        Me.CloseLink.ForeColor = Color.Crimson
    End Sub

    Private Sub CloseLink_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles CloseLink.MouseLeave
        Me.CloseLink.ForeColor = Me.ForeColor
    End Sub

    Private Sub TabPageControl_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        Me.Pages.CurrentPage = Me.Page
    End Sub
#End Region

#Region "Rendering"
    Public Property IsCurrent() As Boolean
        Get
            Return Me.CloseLink.Visible
        End Get
        Set(ByVal value As Boolean)
            If (value <> Me.CloseLink.Visible) Then
                Me.CloseLink.Visible = value
                Me.Invalidate()
            End If
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        If (Me.Pages Is Nothing) Then Exit Sub

        ' If we're the current page, paint a big tab.
        If (Me.Pages.CurrentPage Is Me.Page) Then
            Me.PaintBackground(e, New Rectangle(0, 0, Me.Width, Me.Height), _
                New RectangleF(0, 0, Me.Width, Me.Height / 2), _
                AddColor(Me.Pages.TabColor, 100), _
                Me.Pages.TabColor, True)
        Else
            ' Otherwise, do what we always do.
            MyBase.OnPaint(e)
        End If

        ' Now we'll paint the text.
        Dim fmt As New StringFormat(StringFormatFlags.NoWrap)
        fmt.Alignment = StringAlignment.Near
        fmt.LineAlignment = StringAlignment.Center
        fmt.Trimming = StringTrimming.EllipsisCharacter

        Using textBrush As New SolidBrush(Me.ForeColor)
            If (Me.Pages.CurrentPage Is Me.Page) Then
                e.Graphics.DrawString(Me.Text, Me.Font, textBrush, New RectangleF(TopMargin, 0, Me.Width - Me.CloseLink.Width - TopMargin, Me.Height), fmt)
            Else
                e.Graphics.DrawString(Me.Text, Me.Font, textBrush, New RectangleF(TopMargin, 0, Me.Width - 1 - TopMargin, Me.Height), fmt)
            End If
        End Using
    End Sub
#End Region

#Region "Tooltips"
    Private myToolTip As String
    Public Property ToolTip() As String
        Get
            Return myToolTip
        End Get
        Set(ByVal value As String)
            myToolTip = value
            Me.SetTooltip()
        End Set
    End Property

    Private Sub SetTooltip()
        If (Me.Page.Parent IsNot Nothing) Then
            Me.Page.Parent.ToolTips.SetToolTip(Me, Me.Text & vbNewLine & Me.ToolTip)
            Me.Page.Parent.ToolTips.SetToolTip(Me.CloseLink, "Close page.")
        End If
    End Sub

    Private Sub Page_ParentChanged() Handles Page.ParentChanged
        Me.SetTooltip()
    End Sub

    Private Sub TabPageControl_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
        Me.SetTooltip()
    End Sub
#End Region

End Class
